// packageG/pages/diyCategory/diyCategory.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_id: '',
    shareInfo: {},
    codeShow:false,
    acp:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(',');
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split('=');
          console.log(chil_arr);
          if (chil_arr[0] == 'page_id') {
            this.setData({
              page_id: chil_arr[1]
            });
          }
          if (chil_arr[0] == 'mid') {
            app._setMid(chil_arr[1]);
          }
          if (chil_arr[0] == 'acp') {
            this.setData({
              acp:true
            })
            this.getData();
          }
        }
      }
    }
    if (options.mid) {
      app._setMid(options.mid);
    }
    if (options.page_id) {
      this.setData({
        page_id: options.page_id
      })
      this.getData();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      title: this.data.shareInfo.title,
      path: '/packageG/pages/diyCategory/diyCategory?page_id=' + this.data.page_id + '&mid=' + mid,
      imageUrl: this.data.shareInfo.icon
    }
  },
  // 商品列表添加刷新 购物车组件
  AddCart() {
    let categoryCart = this.selectComponent('#categoryCart')
    if (categoryCart) {
      categoryCart.getCart();
    }
  },
  // 购物车变动刷新 商品列表购物车数量
  cartShowClose(e) {
    if (e.detail) {
      console.log('购物车弹出close')
      let categorySort = this.selectComponent('#categorySort')
      if (categorySort) {
        categorySort.updateData();
      }
    }
  },
  getData() {
    let urlstr = app.getNetAddresss('home-page.index');
    let jsons = {
      page_id: this.data.page_id,
    };
    app._getNetWork({
      url: urlstr,
      data: jsons,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          if(this.data.acp){
            this.address_codeMethod(res.data.basic_info);
          }
          let designerItem = res.data.item;
          this.setData({
            components: designerItem.datas,
          })
          let page = res.data.item.page_info;
          this.data.shareInfo = {
            title: page.page_title,
            icon: page.page_icon,
            des: page.page_description
          }
          wx.setNavigationBarTitle({
            title: res.data.item.page_name,
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: (res) => {
        console.log(res);
      }
    });
  },
  address_codeMethod(basic_infoData = wx.getStorageSync('yz_basic_info')){
    let basic_info = basic_infoData;
    if(basic_info.plugin_setting.address_code && basic_info.plugin_setting.address_code.enable){
      this.addressPopup();
    }
  },
  closeChild(){
    this.setData({
      codeShow:false
    })
  },
  onClickLeft(){
    this.setData({
      codeShow:false
    })
  },
  addressPopup(){
    this.setData({
      codeShow:true,
    })
    setTimeout(async() => {
      let address = this.selectComponent('#yz_showAddress');
      address.setData({
        addressCodeHospital:false,
        HospitalForm:{}
      })
      let acinfo = await address.getDefaultAddress();
      if(acinfo){
        address.code_editAddress(acinfo);
        address.getIsOpenAddress();
      }
    }, 1000);
  },
})